package cn.gson.school.controller.qiantai;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.qiantai.LearnPlanDao;
import cn.gson.school.dao.qiantai.TeamStudentDao;
import cn.gson.school.entity.jiaoxue.LearnplanEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.jiaoxue.TeamStudentEntity;
import cn.gson.school.mappers.qiantai.LearnPlanMapper;

/**
 * 前台计划Controller
 */
@Controller
public class LearnplanController {
	@Autowired
	private LearnPlanMapper planMapper;

	@Autowired
	private LearnPlanDao planDao;

	@Autowired
	private StudentDao stuDao;
	
	@Autowired
	private TeamStudentDao teamstuDao;

	/**
	 * 查询列表
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("qt-learnplan-list")
	public String form(Model model, HttpServletRequest request) throws ParseException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			List<LearnplanEntity> list = planMapper.listPlan(id);
			if (list != null) {
				System.out.println("计划列表：" + list);
				model.addAttribute("planlist", list);
			}
		}
		return "qt-learnplan-my";
	}

	/**
	 * 计划详情
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("qt-plan")
	public String form1(Model model, HttpServletRequest request) throws ParseException {
		Integer week = Integer.parseInt(request.getParameter("week"));
		Long id = Long.parseLong(request.getParameter("planId"));
		if (week != null && id != null) {
			LearnplanEntity plan = planDao.findOne(id);
			List<LearnplanEntity> planEntity = planDao.findByWeek(week);
			if (planEntity != null && plan != null) {
				System.out.println("周计划列表：" + planEntity);
				model.addAttribute("planlist", planEntity);
				model.addAttribute("plan", plan);
			}
		}
		return "qt-learnplan-details";
	}

	/**
	 * 计划表单
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("qt-learnplan-add")
	public String form(HttpServletRequest request, Model model) {
		Integer weeks = Integer.parseInt(request.getParameter("week"));
		Long stuid = Long.parseLong(request.getParameter("stuId"));
		String years=(new SimpleDateFormat("yyyy")).format(new Date());
		if (weeks != null && stuid != null) {
			StudentEntity student=stuDao.findOne(stuid);
			List<LearnplanEntity> planEntity = planDao.findByWeekAndStudent(weeks,student);
			if (planEntity != null) {
				System.out.println("添加计划列表：" + planEntity);
				model.addAttribute("planEntity", planEntity);
				model.addAttribute("week", weeks);
				model.addAttribute("year", years);
			}
		}else {
			Calendar cl = Calendar.getInstance();
			cl.setTime(new Date());
			int week = cl.get(Calendar.WEEK_OF_YEAR);
			cl.add(Calendar.DAY_OF_MONTH, -7);
			int year = cl.get(Calendar.YEAR);
			if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
				year += 1;
			}
			model.addAttribute("week", week);
			model.addAttribute("year", year);
			System.out.println(year + "年第" + week + "周");
		}
		return "qt-learnplan-publish";
	}

	@GetMapping("qt-plan-success")
	public String success() throws ParseException {
		return "plansuccess";
	}
	
	/**
	 * 添加跟进信息
	 * 
	 * @throws ParseException
	 * @throws IOException
	 * @throws ServletException
	 */
	@PostMapping({ "plan-publish"})
	@ResponseBody
	public void save(@Valid LearnplanEntity planEntity, BindingResult br, HttpServletRequest request,
			HttpServletResponse response) throws ParseException, ServletException, IOException {
		/* 添加，编辑所需获取的信息 */
		Long stuId = Long.parseLong(request.getParameter("stuId"));// 获取计划第几周
		String planContent[] = request.getParameterValues("planContent");// 获取计划内容
		String planStartTime[] = request.getParameterValues("planStartTime");// 获取计划开始时间
		String planEndTime[] = request.getParameterValues("planEndTime");// 获取计划结束时间
		String planSupport[] = request.getParameterValues("planSupport");// 获取计划所需支持
		String planCompletion[] = request.getParameterValues("planCompletion");// 获取计划执行状态
		// 编辑的时的两种情况
		// 1、有信息，更新信息
		for(int i=0;i<planContent.length;i++){
			LearnplanEntity plan=new LearnplanEntity();
			Integer week = Integer.parseInt(request.getParameter("week"));// 获取计划第几周
			StudentEntity stuEntity = stuDao.findOne(stuId);// 找到当前用户
			Date planTime = new Date();// 获取计划时间 
			plan.setPlanContent(planContent[i]);
			plan.setPlanStartTime(/*new SimpleDateFormat("yyyy-MM-dd").parse(*/planStartTime[i]/*)*/);
			plan.setEndTime(/*new SimpleDateFormat("yyyy-MM-dd").parse(*/planEndTime[i]/*)*/);
			plan.setNeedSupport(planSupport[i]);
			plan.setCompletion(planCompletion[i]);
			plan.setMakeTime(planTime);
			plan.setWeek(week);
			plan.setStudent(stuEntity);
			planDao.save(plan);
		}
		response.sendRedirect("qt-plan-success");
	}

	@GetMapping("qt-crew")
	public void formcrew(Model model, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			List<TeamStudentEntity> teamstudent=teamstuDao.findByStudentId(id);
			System.out.println("身份:"+teamstudent.get(0).getStuidentity());
			if(teamstudent.get(0).getStuidentity().equals("组长")) {
				List<LearnplanEntity> planEntity = planMapper.teamlistPlan();
				System.out.println("11111111组员计划列表："+planEntity);
				if(planEntity!=null) {
					System.out.println("组员计划列表："+planEntity);
					request.setAttribute("planlist", planEntity);
				}else {
					System.out.println("组员计划列表是空的！");
				}
			}
		}
		request.getRequestDispatcher("qt-learnplan-crew").forward(request, response);
	}
	
	@GetMapping("qt-deleteplan-success")
	public String deletesuccess() throws ParseException {
		return "deletesuccess";
	}
	
	@PostMapping("qt-delete")
	public void delete(Model model, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		System.out.println("到删除计划controller了");
		Long id = Long.parseLong(request.getParameter("id"));
		if (id != null) {
			planDao.delete(id);
		}
		response.sendRedirect("qt-deleteplan-success");
	}
}
